﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SchoolsBI;

public partial class Alumnos_alumnos : System.Web.UI.Page
{
    static Alumnos oAlumnos = new Alumnos();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { FillGrid(); }
    }
    protected void cmdAccion_Click(object sender, EventArgs e)
    {
        Save();
        FillGrid();
    }

    private void FillGrid()
    {
        GridView1.DataSource = Alumnos.Tabla("select IdAlumno, Apellido, Nombre from dbo.Alumnos order by Apellido");
        GridView1.DataKeyNames = new string[] { "IdAlumno" };
        GridView1.AutoGenerateColumns = false;
        GridView1.DataBind();
        cmdAccion.Text = "Nuevo";
        Clear();

    }

    void Save(){
        switch (cmdAccion.Text) { 
            case "Nuevo":
                FormToObjet();
                oAlumnos.Crear();
                break;
            case "Modificar":
                FormToObjet();
                oAlumnos.Actualizar();
                break;
            case "Eliminar":
                oAlumnos.Borrar();
                break;
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        oAlumnos= Alumnos.Buscar("IdAlumno=" + GridView1.SelectedValue.ToString());
        showData();
        cmdAccion.Text = "Modificar";
    }
    
    void showData(){
        ObjetToForm();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        oAlumnos = Alumnos.Buscar("IdAlumno=" + GridView1.DataKeys[e.RowIndex].Value);
        showData();
        cmdAccion.Text = "Eliminar";
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    void FormToObjet()
    {
        //oAlumnos.IdAlumno = txtIdAlumno.Text;
        oAlumnos.Apellido = txtApellido.Text;
        oAlumnos.Nombre = txtNombre.Text;
        oAlumnos.Domicilio = txtDomicilio.Text;
        oAlumnos.Telefono = txtTelefono.Text;
        oAlumnos.Email = txtEmail.Text;
        if (txtNroDocumento.Text.Length > 0)
        {
            oAlumnos.NroDocumento = Convert.ToInt32(txtNroDocumento.Text);
            oAlumnos.IdTipoDoc = TipoDocumento1.Value;
            oAlumnos.FechaNacimiento = txtFechaNacimiento.Text;
        }
        oAlumnos.Activo = 1;
    }
    void ObjetToForm()
    {
        txtIdAlumno.Text = Convert.ToString(oAlumnos.IdAlumno);
        txtApellido.Text = oAlumnos.Apellido;
        txtNombre.Text = oAlumnos.Nombre;
        txtDomicilio.Text = oAlumnos.Domicilio;
        txtTelefono.Text = oAlumnos.Telefono;
        txtEmail.Text = oAlumnos.Email;
        txtNroDocumento.Text = Convert.ToString(oAlumnos.NroDocumento);
        TipoDocumento1.Value = oAlumnos.IdTipoDoc;
        txtFechaNacimiento.Text = oAlumnos.FechaNacimiento;
        txtActivo.Text = "1";
    }
    void Clear()
    {
        txtIdAlumno.Text = "";
        txtApellido.Text = "";
        txtNombre.Text = "";
        txtDomicilio.Text = "";
        txtTelefono.Text = "";
        txtEmail.Text = "";
        txtNroDocumento.Text = "";
        TipoDocumento1.Value = "-1";
        txtFechaNacimiento.Text = "";
        txtActivo.Text = "1";
    }


}
